import isEmpty from 'loadsh/isEmpty'
Page({
  data: {
    item: 1,
    min: null,
    max: new Date('2033/12/02'),
    defaultDate: new Date(),
    deviceinfo: {
      CorpId: '',//申请人
      BarcodeNumber: '',//	扫码枪编号
      AttributionEngineer: '',//归属工程师
      SampleProcessTime: '',//	样品处理时间
      SampleHandler: '',//	样品处理人
      DeviceUsageContent: '',//设备使用内容
      DeviceUsageTime: '',//设备使用时间
      EquipmentUsers: '',//设备使用人
      InternalDuration: '',//	内部时长统计
      ExternalDuration: '',//	外部时长统计
    },
    ReceiveData: [],
  },
  Delete(e) {
    const ID = e.target.dataset.item.id
    PostDeleteData(ID, 'BusinessTrip', (res) => {
      my.alert({
        title: '撤销成功',
      });
      //刷新数据
      this.setData({
        ReceiveData: this.data.ReceiveData.filter((it) => {
          return it.id !== ID
        })
      })
    }, (err) => {
      my.alert({
        title: '撤销失败',
      });
    })
  },
  handleOk1(date, format, e) {
    this.setData({
      deviceinfo: {
        ...this.data.deviceinfo,
        StartDate: date
      },
      min: date
    })
  },
  OnTravelType(value) {
    this.setData({
      deviceinfo: {
        ...this.data.deviceinfo,
        TravelType: value
      }
    })
  },
  // 上传数据
  onSubmit(e) {
    // console.log("提交出差数据", e),
    Postdeviceinfo(JSON.stringify({
      corpId: this.data.deviceinfo.CorpId,
      type: this.data.deviceinfo.TravelType,
      destination: this.data.deviceinfo.Destination,
      departure: this.data.deviceinfo.PlaceDeparture,
      vehicle: this.data.deviceinfo.Vehicle,
      members: this.data.deviceinfo.Colleague,
      startTime: this.data.deviceinfo.StartDate,
      endTime: this.data.deviceinfo.EndDate,
      description: this.data.deviceinfo.BusinessTrip,
      annex: this.data.deviceinfo.Annex,
    }), (res) => {
      my.alert({
        title: '提交成功',
      });
      Getdeviceinfo((res) => {
        this.setData({
          ReceiveData: res.data.data
        })
      }, this.data.deviceinfo.CorpId)
      console.log('返回出差数据1', this.data.deviceinfo);
      //清除填写数据
      this.setData({
        deviceinfo: {
          ...this.data.deviceinfo,
          TravelType: '请点击选择出差类型',
          TypeDependent: '', //类型相关
          PlaceDeparture: '', //出发地
          Destination: '', //目的地
          Vehicle: '', //交通工具
          Colleague: [], //同行人名字
          StartDate: null, //开始时间
          EndDate: null, //结束时间
          SumTime: 0, //时长
          BusinessTrip: '', //出差事由
          Annex: []//附件
        }
      })
    }, (err) => {
      my.alert({
        title: '提交成功1',
      });
    })
  },
  //上传图片
  onUpload(file) {
    const filePath = file.path
    return new Promise((resolve, reject) => {
      PostInfoPicture(filePath, (res) => {
        this.setData({
          deviceinfo: {
            ...this.data.deviceinfo,
            Annex: [...this.data.deviceinfo.Annex, ...JSON.parse(res.data).data]
          }
        })
      })
    })

  },
  onRemove(file) {
    return new Promise((resolve) => {
      console.log('即将移除的图片为：', file);
      my.confirm({
        title: '移除图片',
        content: '是否确认移除图片',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (e) => {
          resolve(e.confirm);
        }
      });
    })
  },
  onBeforeUpload(localFileList) {
    console.log('即将上传的图片列表为：', localFileList);
    localFileList = localFileList.filter(item => item.size < 10000);
    console.log('修改上传的图片列表为：', localFileList);
    return localFileList;
  },
  onChooseImageError(err) {
    console.log(err);
  },
  bindButtonTap() {
    this.onFocus();
  },
  //获取数据
  onLoad() {
    my.getStorage({
      key: 'userInfo',
      success: (res) => {
        Getdeviceinfo((res) => {
          this.setData({
            ReceiveData: res.data.data
          })
        }, res.data.result.userid)
        console.log('返回出差数据2', this.data.ReceiveData);

        this.setData({
          deviceinfo: {
            ...this.data.deviceinfo,
            CorpId: res.data.result.userid
          }
        })
      }
    })
  },
});